package com.geostar.constructionland.analyse.service.handler.impl;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.entity.HttpResult;
import com.geostar.constructionland.analyse.entity.alyresult.AlyAreaCalResult;
import com.geostar.constructionland.analyse.entity.model.AnalyseModel;
import com.geostar.constructionland.analyse.entity.resulthandler.HandlerResult;
import com.geostar.constructionland.analyse.service.handler.IResultHandler;
import com.geostar.constructionland.analyse.service.handler.impl.util.XmlUtil;
import com.geostar.constructionland.common.entity.biz.PieceLandInfo;
import com.geostar.constructionland.common.service.biz.PieceLandInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author yelll
 * @since 2020-5-29 16:10
 * 面积计算处理
 **/
@Component
@Slf4j
public class AreaCalHandler implements IResultHandler {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    PieceLandInfoService pieceLandInfoService;
    @Override
    public HandlerResult handler(AnalyseModel analyseModel, HttpResult httpResult) throws IOException {
        log.info("处理面积计算返回数据,状态码:{}",httpResult.getCode());
        HandlerResult result;
        if(HttpStatus.SC_OK==httpResult.getCode()){
            String newBody = XmlUtil.parseXml(httpResult.getBody());
            httpResult.setBody(newBody);
            /**保存面积计算结果**/
            AlyAreaCalResult alyAreaCalResult = objectMapper.readValue(httpResult.getBody(), AlyAreaCalResult.class);
            if(null!=alyAreaCalResult.getPieceLandInfoList() && alyAreaCalResult.getPieceLandInfoList().size() > 0){
                for(PieceLandInfo landInfo : alyAreaCalResult.getPieceLandInfoList()){
                    LambdaUpdateWrapper<PieceLandInfo> updateWrapper = new LambdaUpdateWrapper<>();
                    updateWrapper.eq(PieceLandInfo::getDkid,landInfo.getDkid())
                            // 吉威返回的DKMJ对应的是我们JSYD_DKXX表中的DKMJYS字段
                            .set(PieceLandInfo::getDkmjYs,landInfo.getDkmj())
                            .set(PieceLandInfo::getDkmj,0);
                    pieceLandInfoService.update(updateWrapper);
                }
            }
            /// TODO: 2020-7-9 历史案卷，即_BA结尾的案卷需要做平差分析
            result = HandlerResult.createBySuccess(httpResult);
        }else{
            result = HandlerResult.createBySpatiaAnalyse_error("调用分析失败，请查看分析结果内容",httpResult);
        }
        return result;
    }
}
